﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.Contract;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using CommonsLib.Settings;
using System.Data;

using System.Security.Cryptography;
using MaxLeaf.Platform.Functions;
using MaxLeaf.Platform.Commons;

namespace MaxLeaf.Platform.DALImplementation
{
    public class poReqDtlDAL :BaseDAL 
    {


        public void SetPosted(DBAccess dbcon, int intKey, int intOrderHdrId, int intOrderDtlId)
        {
            string strSql = "update poReqDtl set posted=1  where DtlID=@FInterID and posted=0 and is_delete=0 ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intKey.ToString())
 
            });
        }

        public List<int> GetUnpostKeys(DBAccess dbcon, int intHdrID)
        {
            List<int> lst = new List<int>();
            string strSql = "select DtlID from poReqDtl where HdrID=@HdrID and posted=0 and is_delete=0 ";
            using (DbDataReader rdr = dbcon.GetDataReader(strSql, new SqlParameter[] { new SqlParameter("@HdrID", intHdrID.ToString())
            }))
            {
                while (rdr.Read())
                {
                    lst.Add(rdr.GetInt32(0));
                }
                
            }

            return lst;
        }

        internal static string QryTableName(object model)
        {
            string strSql = string.Format(@"
                (
                    select D.*                    
                    ,isnull(g_k.usegoods_name,'') as Keywords                    
                    ,g.sku_num as SKU
                    ,dep.DepName
                    
                    from {0}poReqHdr H
                    inner join  {0}poReqDtl D  on h.finterid=d.hdrid
                    inner join {1}ImInvMaster i on d.itemid=i.FinterID                    
                    inner join {1}goods_main g on g.goods_id=i.GoodsID
                    inner join {1}mDepartment dep on dep.depid=D.DepID
                    
                    left join {1}goods_UserKeywords g_k on g_k.company_id=D.CompanyID and i.FinterID=g_k.ItemID                    
                    where h.posted=0 and d.posted=0 and h.is_delete=0 and d.is_delete=0
                    
                ) a"
                    , GetOrdDatabase()
                    , GetBaseDatabase()
                    , GetInvDatabase()
                    );

            return strSql;
        }
    }
}
